<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>青云教育系统 - 教师端</title>
    <link rel="stylesheet" href="../../teacher.css">
</head>
<body>
    <div class="layout">
        <aside class="sidebar">
            <div class="brand">青云教育 · 教师端</div>
            <nav class="menu" id="menu">
                <button class="menu-item" data-view="profile" data-url="/index">教员信息及考勤</button>
                <button class="menu-item active" data-view="schedule" data-url="/schedule">教师课表</button>
                <button class="menu-item" data-view="password" data-url="/password">修改密码</button>
                <button class="menu-item" data-view="students" data-url="/students">学生信息</button>
                <button class="menu-item" data-view="upload" data-url="/upload">上传资料</button>
                <button class="menu-item" data-view="download" data-url="/download">下载资料</button>
                <button class="menu-item" data-view="mymaterials" data-url="/mymaterials">我的资料</button>
                <button class="menu-item" data-view="feedback" data-url="/feedback">课堂反馈</button>
                <button class="menu-item" data-view="myfeedback" data-url="/myfeedback">我的反馈</button>
            </nav>
        </aside>
        <main class="content">
            <header class="topbar">
                <div class="left">
                    <h1 id="view-title">教师课表</h1>
                </div>
                <div class="right">
                    <div class="user-info">
                        <img id="user-avatar" class="user-avatar" src="" alt="用户头像">
                        <span id="teacher-name" class="user-name"></span>
                    </div>
                    <button id="logoutBtn" class="btn btn-secondary">退出登录</button>
                </div>
            </header>

            <section id="view-schedule" class="view" style="display:block;">
                <div class="card">
                    <div class="card-header">
                        <div class="filters">
                            <label>
                                选择日期：
                                <input type="date" id="datePicker">
                            </label>
                            <button id="refreshSchedule" class="btn btn-primary">刷新课表</button>
                        </div>
                    </div>
                    <div class="card-body">
                        <div id="scheduleContainer" class="schedule-table-wrapper">
                            <div class="week-schedule">
                                <div class="week-header">
                                    <div class="week-title">周课表</div>
                                    <div class="week-date-range" id="weekDateRange"></div>
                                </div>
                                <div class="schedule-grid">
                                    <div class="schedule-row header-row">
                                        <div class="schedule-cell header-cell">时间</div>
                                        <div class="schedule-cell header-cell" id="monday">周一</div>
                                        <div class="schedule-cell header-cell" id="tuesday">周二</div>
                                        <div class="schedule-cell header-cell" id="wednesday">周三</div>
                                        <div class="schedule-cell header-cell" id="thursday">周四</div>
                                        <div class="schedule-cell header-cell" id="friday">周五</div>
                                        <div class="schedule-cell header-cell" id="saturday">周六</div>
                                        <div class="schedule-cell header-cell" id="sunday">周日</div>
                                    </div>
                                    <div class="schedule-row">
                                        <div class="schedule-cell time-cell">08:00-09:35</div>
                                        <div class="schedule-cell" id="monday-1"></div>
                                        <div class="schedule-cell" id="tuesday-1"></div>
                                        <div class="schedule-cell" id="wednesday-1"></div>
                                        <div class="schedule-cell" id="thursday-1"></div>
                                        <div class="schedule-cell" id="friday-1"></div>
                                        <div class="schedule-cell" id="saturday-1"></div>
                                        <div class="schedule-cell" id="sunday-1"></div>
                                    </div>
                                    <div class="schedule-row">
                                        <div class="schedule-cell time-cell">10:00-11:35</div>
                                        <div class="schedule-cell" id="monday-2"></div>
                                        <div class="schedule-cell" id="tuesday-2"></div>
                                        <div class="schedule-cell" id="wednesday-2"></div>
                                        <div class="schedule-cell" id="thursday-2"></div>
                                        <div class="schedule-cell" id="friday-2"></div>
                                        <div class="schedule-cell" id="saturday-2"></div>
                                        <div class="schedule-cell" id="sunday-2"></div>
                                    </div>
                                    <div class="schedule-row">
                                        <div class="schedule-cell time-cell">14:00-15:35</div>
                                        <div class="schedule-cell" id="monday-3"></div>
                                        <div class="schedule-cell" id="tuesday-3"></div>
                                        <div class="schedule-cell" id="wednesday-3"></div>
                                        <div class="schedule-cell" id="thursday-3"></div>
                                        <div class="schedule-cell" id="friday-3"></div>
                                        <div class="schedule-cell" id="saturday-3"></div>
                                        <div class="schedule-cell" id="sunday-3"></div>
                                    </div>
                                    <div class="schedule-row">
                                        <div class="schedule-cell time-cell">16:00-17:35</div>
                                        <div class="schedule-cell" id="monday-4"></div>
                                        <div class="schedule-cell" id="tuesday-4"></div>
                                        <div class="schedule-cell" id="wednesday-4"></div>
                                        <div class="schedule-cell" id="thursday-4"></div>
                                        <div class="schedule-cell" id="friday-4"></div>
                                        <div class="schedule-cell" id="saturday-4"></div>
                                        <div class="schedule-cell" id="sunday-4"></div>
                                    </div>
                                </div>
                            </div>
                            <div id="scheduleEmpty" class="empty" style="display:none;">暂无课程安排</div>
                        </div>
                    </div>
                </div>
            </section>
        </main>
    </div>

    <script src="../../teacher.js"></script>
    <script>
        // 页面加载时验证登录状态
        document.addEventListener('DOMContentLoaded', function(){
            const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
            
            // 检查用户是否已登录
            if (!userInfo || !userInfo.token || !userInfo.userDTO || 
                typeof userInfo.token !== 'string' || userInfo.token.trim() === '') {
                console.log('用户未登录，跳转到登录页');
                localStorage.removeItem('userInfo');
                localStorage.removeItem('userToken');
                window.location.href = '../../login.html';
                return;
            }
            
            // 验证token格式
            if (userInfo.token.length < 10) {
                console.log('Token格式无效，跳转到登录页');
                localStorage.removeItem('userInfo');
                localStorage.removeItem('userToken');
                window.location.href = '../../login.html';
                return;
            }
            
            console.log('用户已登录，页面正常加载');
        });
        
        // 确保退出登录功能正常工作
        document.addEventListener('DOMContentLoaded', function(){
            // 绑定退出登录按钮事件（如果teacher.js没有绑定成功）
            const logoutBtn = document.getElementById('logoutBtn');
            if (logoutBtn && !logoutBtn.hasAttribute('data-bound')) {
                logoutBtn.setAttribute('data-bound', 'true');
                logoutBtn.addEventListener('click', function(e) {
                    console.log('退出登录按钮被点击（来自schedule页面）');
                    e.preventDefault();
                    if (typeof logout === 'function') {
                        logout();
                    } else {
                        console.error('logout函数未定义');
                    }
                });
            }
            
            // 菜单跳转逻辑
            const menu = document.getElementById('menu');
            menu.addEventListener('click', function(e){
                const btn = e.target.closest('.menu-item');
                if(!btn) return;
                const view = btn.getAttribute('data-view');
                if(view === 'students') window.location.href = '/teacher/students';
                else if(view === 'password') window.location.href = '/teacher/password';
                else if(view === 'profile') window.location.href = '/index.html';
                else if(view === 'upload') window.location.href = '/teacher/postmaterial';
                else if(view === 'download') window.location.href = '/teacher/downloadmaterial';
                else if(view === 'mymaterials') window.location.href = '/teacher/mymaterials/';
                else if(view === 'feedback') window.location.href = '/teacher/feedback';
                else if(view === 'myfeedback') window.location.href = '/teacher/myfeedback';
            });
        });
    </script>
</body>
</html>

